Appendix D

C++ Language Guidelines


A. General
  1. Use consistent indentation of approximately 3-4 spaces.
  2. Line up a closing bracket, ‘}’, with the corresponding open bracket, ‘{‘.
  3. Use whitespace to improve the readability of your program. Blank lines are to be encouraged. At a minimum, blank lines should separate declarations, input, different kinds of processing and output. (This is not to imply that programs should be double spaced.)
  4. In arithmetic statements spaces should be used to separate variables and operators, for example:
    A = X + Y;
    not
    A=X+Y;
  5. For arrays no space between array name and entry, for example:
    A[I]
    not
    A [I] or A [ I ]
  6. Only one statement is permitted on a line.

B. Comments

1. The program heading should occur at the top of the program and should include:

/*
NAME OF PROGRAMMER: DATE:
ONE SENTENCE DESCIPTION OF PROGRAM
FILE NAME


NARRATIVE DESCRIPTION:
    :

INTERFACE ANALYSIS:
    :

CONSTANTS:

*/

2. Each function heading should be followed by the following:

/*
PURPOSE:
RECEIVES:
RETURNS:
*/

3. Comments should in general be on separate lines but very short comments are acceptable at the end of a line, for example:

int GetValue();   // Return the current value

 

II. Identifiers and the Use of Uppercase Letters.

  1. Variable and function names should be meaningful. Variable names should be in lower-case except for the first letters of all words after the first, for example:
         thisIsAVariableName

  2. Class declarations and user-defined types should begin with a capital letter, for example:
        ThisIsAnObject

  3. Functions should have each word capitalized, for example:
        ThisIsAFunctionName

  4. Non-trivial constants must be declared and written captitalized, for example:
        const double SQRT2 = 1.414;

  5. Multi-word constants should use underscore between words.
        const int MAX_NUMBER_OF_THINGS = 20;

III. Style for blocks, indentation and related comments:

Examples:
A. The basic if statement
     if (condition)
{ program statement(s)
} /* endif */

B. A more complex if statement
     if (condition)

{   program statement(s);
}
else
{    if (condition2)
      {       program statement(s);
      }
    else
      {      program statement(s)
     }
}  // endif

C. The switch statement
    switch (expression)

  {  case value1:
           program statement(s);
           break;

      case value2:
           program statement(s);
           break;
		   
      // other cases
	  
      default:
          program statement(s);
          break;
  } //endswitch

D. The while statement

     while (expression)
     {   program statement(s); 
     }
	  

E. The for statement

       for (init_expression; loop_condition; loop_expression) 
       {    program statements;  
       }

F. The do-while statement

      do
         program statement(s);
      while (expression);

G. Function declarations

   int function ThisIsAFunction (int par1, int par2);
    {
       program statement(s);
    } 

         
    
        
     

 

V. Other Style Issues

  1. All functions should be written to perform one and only one major task.
  2. Avoid the use of goto’s and labels.